﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Rectangle = Microsoft.Xna.Framework.Rectangle;
using Microsoft.Xna.Framework.Graphics;
using System.Windows.Forms;
using Brain.Editor;

namespace Brain.Picking
{
    public class BoxScale
    {
        public Rectangle Target;
        public event SetRectangle SetTarget;

        public Rectangle Top,
            Bottom,
            Left,
            Right,
            TopLeft,
            TopRight,
            BottomLeft,
            BottomRight;

        public bool b_Top,
            b_Bottom,
            b_Left,
            b_Right,
            b_TopLeft,
            b_TopRight,
            b_BottomLeft,
            b_BottomRight;

        public SpriteBatch sBatch;
        public Texture2D Pixel;

        public Control Father;
        public IPickingManager2D Manager;

        public BoxScale(Control father, IPickingManager2D manager)
        {
            this.Manager = manager;
            this.Father = father;
            Target = new Rectangle(0, 0, 256, 256);

            if (Engine.Instance != null)
            {
                sBatch = new SpriteBatch(Engine.Instance.GraphicsDevice);
                Pixel = new Texture2D(Engine.Instance.GraphicsDevice, 1, 1);
                Pixel.SetData(new Color[] { Color.White });
            }

            Top = new Rectangle(0, 0, 6, 6);
            Bottom = new Rectangle(0, 0, 6, 6);
            Left = new Rectangle(0, 0, 6, 6);
            Right = new Rectangle(0, 0, 6, 6);
            TopLeft = new Rectangle(0, 0, 6, 6);
            TopRight = new Rectangle(0, 0, 6, 6);
            BottomLeft = new Rectangle(0, 0, 6, 6);
            BottomRight = new Rectangle(0, 0, 6, 6);
        }

        public void Draw()
        {
            if (sBatch == null && Engine.Instance != null)
            {
                sBatch = new SpriteBatch(Engine.Instance.GraphicsDevice);
                Pixel = new Texture2D(Engine.Instance.GraphicsDevice, 1, 1);
                Pixel.SetData(new Color[] { Color.White });
            }
            if (Engine.Instance == null)
            {
                return;
            }

            ICamera2D camera = null;
            if (Engine.Instance.CameraManager != null &&
                Engine.Instance.CameraManager.Camera2D != null)
            {
                camera = Engine.Instance.CameraManager.Camera2D;
                camera = null;
            }

            Vector2 mouse = Engine.Instance.InputManager.GetMousePosition();
            if (Engine.Instance.CameraManager != null &&
                Engine.Instance.CameraManager.Camera2D != null)
            {
                Matrix m = Matrix.Invert(Engine.Instance.CameraManager.Camera2D.World);
                mouse = Vector2.Transform(mouse, m);
            }
            
            Rectangle mouseRect = new Rectangle((int)mouse.X, (int)mouse.Y, 1, 1);
            bool mouseClicking = Engine.Instance.InputManager.IsMouseLeftDown();
            bool mouseNotClicking = Engine.Instance.InputManager.IsMouseLeftUp();

            Father.Cursor = Cursors.Default;

            if (this.Target.Intersects(mouseRect))
            {
                Father.Cursor = Cursors.SizeAll;
            }

            if (!Manager.Manipulating)
            {
                if (Top.Intersects(mouseRect))
                {
                    Father.Cursor = Cursors.SizeNS;

                    if (mouseClicking)
                    {
                        b_Top = true;
                    }
                }
                else if (Bottom.Intersects(mouseRect))
                {
                    Father.Cursor = Cursors.SizeNS;

                    if (mouseClicking)
                    {
                        b_Bottom = true;
                    }
                }
                else if (Left.Intersects(mouseRect))
                {
                    Father.Cursor = Cursors.SizeWE;

                    if (mouseClicking)
                    {
                        b_Left = true;
                    }
                }
                else if (Right.Intersects(mouseRect))
                {
                    Father.Cursor = Cursors.SizeWE;

                    if (mouseClicking)
                    {
                        b_Right = true;
                    }
                }
                else if (TopLeft.Intersects(mouseRect))
                {
                    Father.Cursor = Cursors.SizeNWSE;

                    if (mouseClicking)
                    {
                        b_TopLeft = true;
                    }
                }
                else if (TopRight.Intersects(mouseRect))
                {
                    Father.Cursor = Cursors.SizeNESW;

                    if (mouseClicking)
                    {
                        b_TopRight = true;
                    }
                }
                else if (BottomLeft.Intersects(mouseRect))
                {
                    Father.Cursor = Cursors.SizeNESW;

                    if (mouseClicking)
                    {
                        b_BottomLeft = true;
                    }
                }
                else if (BottomRight.Intersects(mouseRect))
                {
                    Father.Cursor = Cursors.SizeNWSE;

                    if (mouseClicking)
                    {
                        b_BottomRight = true;
                    }
                }

                Vector2 dif = Engine.Instance.InputManager.GetMouseDifference();
                int difX = (int)dif.X;
                int difY = (int)dif.Y;
                Rectangle newRect = this.Target;

                if (b_Top)
                {
                    Father.Cursor = Cursors.SizeNS;
                    newRect = new Rectangle(
                        newRect.X,
                        newRect.Y + difY,
                        newRect.Width,
                        newRect.Height - difY);
                    if (mouseNotClicking)
                    {
                        b_Top = false;
                    }
                }
                else if (b_Bottom)
                {
                    Father.Cursor = Cursors.SizeNS;
                    newRect = new Rectangle(
                        newRect.X,
                        newRect.Y,
                        newRect.Width,
                        newRect.Height + difY);
                    if (mouseNotClicking)
                    {
                        b_Bottom = false;
                    }
                }
                else if (b_Left)
                {
                    Father.Cursor = Cursors.SizeWE;
                    newRect = new Rectangle(
                        newRect.X + difX,
                        newRect.Y,
                        newRect.Width - difX,
                        newRect.Height);
                    if (mouseNotClicking)
                    {
                        b_Left = false;
                    }
                }
                else if (b_Right)
                {
                    Father.Cursor = Cursors.SizeWE;
                    newRect = new Rectangle(
                        newRect.X,
                        newRect.Y,
                        newRect.Width + difX,
                        newRect.Height);
                    if (mouseNotClicking)
                    {
                        b_Right = false;
                    }
                }
                else if (b_TopLeft)
                {
                    Father.Cursor = Cursors.SizeNWSE;
                    newRect = new Rectangle(
                        newRect.X + difX,
                        newRect.Y + difY,
                        newRect.Width - difX,
                        newRect.Height - difY);
                    if (mouseNotClicking)
                    {
                        b_TopLeft = false;
                    }
                }
                else if (b_TopRight)
                {
                    Father.Cursor = Cursors.SizeNESW;
                    newRect = new Rectangle(
                       newRect.X,
                       newRect.Y + difY,
                       newRect.Width + difX,
                       newRect.Height - difY);
                    if (mouseNotClicking)
                    {
                        b_TopRight = false;
                    }
                }
                else if (b_BottomLeft)
                {
                    Father.Cursor = Cursors.SizeNESW;
                    newRect = new Rectangle(
                       newRect.X + difX,
                       newRect.Y,
                       newRect.Width - difX,
                       newRect.Height + difY);
                    if (mouseNotClicking)
                    {
                        b_BottomLeft = false;
                    }
                }
                else if (b_BottomRight)
                {
                    Father.Cursor = Cursors.SizeNWSE;
                    newRect = new Rectangle(
                       newRect.X,
                       newRect.Y,
                       newRect.Width + difX,
                       newRect.Height + difY);
                    if (mouseNotClicking)
                    {
                        b_BottomRight = false;
                    }
                }

                if (newRect.Width < 10)
                {
                    newRect.Width = 10;
                }
                if (newRect.Height < 10)
                {
                    newRect.Height = 10;
                }

                if (SetTarget != null)
                {
                    SetTarget(newRect);
                }
            }
            
            Top = RectangleUtil.Position(Top, Target.X + (Target.Width / 2) - (Top.Width / 2), Target.Y - Top.Height);
            Bottom = RectangleUtil.Position(Bottom, Target.X + (Target.Width / 2) - (Bottom.Width / 2), Target.Y + Target.Height);
            Left = RectangleUtil.Position(Left, Target.X - Left.Width, Target.Y + (Target.Height / 2) - (Left.Height / 2));
            Right = RectangleUtil.Position(Right, Target.X + Target.Width, Target.Y + (Target.Height / 2) - (Right.Height / 2));
            TopLeft = RectangleUtil.Position(TopLeft, Target.X - TopLeft.Width, Target.Y - TopLeft.Height);
            TopRight = RectangleUtil.Position(TopRight, Target.X + Target.Width, Target.Y - TopRight.Height);
            BottomLeft = RectangleUtil.Position(BottomLeft, Target.X - BottomLeft.Width, Target.Y + Target.Height);
            BottomRight = RectangleUtil.Position(BottomRight, Target.X + Target.Width, Target.Y + Target.Height);

            if (Engine.Instance.CameraManager != null &&
                Engine.Instance.CameraManager.Camera2D != null)
            {
                sBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, null, null, null, null, Engine.Instance.CameraManager.Camera2D.World);
            }
            else
            {
                sBatch.Begin();
            }

            sBatch.Draw(Pixel, Top, Color.Black);
            sBatch.Draw(Pixel, Bottom, Color.Black);
            sBatch.Draw(Pixel, Left, Color.Black);
            sBatch.Draw(Pixel, Right, Color.Black);
            sBatch.Draw(Pixel, TopLeft, Color.Black);
            sBatch.Draw(Pixel, TopRight, Color.Black);
            sBatch.Draw(Pixel, BottomLeft, Color.Black);
            sBatch.Draw(Pixel, BottomRight, Color.Black);

            sBatch.End();
        }
    }
}
